//
//  NILocalAuthenticationViewController.m
//  gweAD
//
//  Created by navinfo air on 2016/11/8.
//  Copyright © 2016年 mengy. All rights reserved.
//

#import "NILocalAuthenticationViewController.h"
#import <sys/utsname.h>
#import <LocalAuthentication/LAError.h>



@interface NILocalAuthenticationViewController ()

@end

@implementation NILocalAuthenticationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Do any additional setup after loading the view.
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        if (![self isLocalAuthenticationSupport]) {
            biometryType = NIBIOMETRYTYPE_NONE;
        }
    }
    return self;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//压感home键
- (BOOL)isPressureSensitiveHomeButton{
    struct utsname systemInfo;
    uname(&systemInfo);
    
    NSString *deviceModel = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];;
    if (deviceModel.length >= 7) {
        NSString *tmpStr = [[deviceModel substringToIndex:7] lowercaseString];
        if (([tmpStr isEqualToString:@"iphone9"] || [tmpStr isEqualToString:@"iphone10"]) && biometryType != NIBIOMETRYTYPE_FACEID) {
            return YES;
        }
    }
    return NO;
}

- (BOOL)isLocalAuthenticationSupport{
    //判读iPhone型号和iOS系统版本
    if ([UIDevice currentDevice].systemVersion.floatValue < 8.0) {
        return NO;
    }
    
    struct utsname systemInfo;
    uname(&systemInfo);
    
    NSString *deviceModel = [NSString stringWithCString:systemInfo.machine encoding:NSASCIIStringEncoding];;
    
    if ([deviceModel isEqualToString:@"i386"]){
        return NO;
    }
    if ([deviceModel isEqualToString:@"x86_64"]){
        return NO;
    }
    
    
    if (deviceModel.length >= 5) {
        if ([deviceModel.lowercaseString isEqualToString:@"ipad4,8"]){
            return YES;
        }
        
        if ([deviceModel.lowercaseString isEqualToString:@"ipad4,9"]){
            return YES;
        }
        
        NSString *tmpStr = [[deviceModel substringToIndex:5] lowercaseString];
        if ([tmpStr isEqualToString:@"ipod1"] || [tmpStr isEqualToString:@"ipod2"] || [tmpStr isEqualToString:@"ipod3"] || [tmpStr isEqualToString:@"ipod4"] || [tmpStr isEqualToString:@"ipod5"] || [tmpStr isEqualToString:@"ipod6"] || [tmpStr isEqualToString:@"ipod7"] || [tmpStr isEqualToString:@"ipad1"] || [tmpStr isEqualToString:@"ipad2"] || [tmpStr isEqualToString:@"ipad3"] || [tmpStr isEqualToString:@"ipad4"]) {
            return NO;
        }
    }
    
    if (deviceModel.length >= 7) {
        NSString *tmpStr = [[deviceModel substringToIndex:7] lowercaseString];
        if ([tmpStr isEqualToString:@"iphone5"] || [tmpStr isEqualToString:@"iphone4"] || [tmpStr isEqualToString:@"iphone3"]) {
            return NO;
        }
    }
    
    
    
    if (deviceModel.length >= 7) {
        NSString *tmpStr = [[deviceModel substringToIndex:6] lowercaseString];
        if ([tmpStr isEqualToString:@"iphone5"] || [tmpStr isEqualToString:@"iphone4"] || [tmpStr isEqualToString:@"iphone3"]) {
            return NO;
        }
    }
    
    return YES;
}

- (NSInteger)isLocalAuthenticationAble{
    if ([self isLocalAuthenticationSupport]) {
        //验证物理特征
        LAContext *ctx = [[LAContext alloc] init];
        if (@available(iOS 11.0, *)) {
            if (ctx.biometryType == LABiometryTypeFaceID) {
                
            }
        } else {
            // Fallback on earlier versions
        }
        
        
        NSError *error = nil;
        if ([ctx canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
            return 0;
        }
        if (error) {
            return error.code;
        }
    }
    return 0;
}

- (BOOL)policyLocalAuthenticationWithBiometrics{
    return [self policyLocalAuthentication:YES];
}

- (BOOL)policyLocalAuthentication{
    return [self policyLocalAuthentication:NO];
}

- (BOOL)policyLocalAuthentication:(BOOL)showAction{
    LAContext *ctx = [[LAContext alloc] init];
    if([self isLocalAuthenticationAble] == 0){
        NSString *infoText = @"请验证已有的指纹";
        if (biometryType == NIBIOMETRYTYPE_FACEID) {
            infoText = @"请验证当前面容ID";
        }
        [ctx setLocalizedFallbackTitle:@""];
        [ctx evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:infoText reply:^(BOOL success, NSError *error) {
            if (success) {
                [self performSelectorOnMainThread:@selector(localAuthenticationSuccess) withObject:nil waitUntilDone:YES];
            }
            else
            {
                if (error.code == LAErrorAppCancel) {
                }else if (error.code == LAErrorSystemCancel) {
                    //压感home键特殊处理
                    if([self isPressureSensitiveHomeButton]){
                        [self performSelectorOnMainThread:@selector(localAuthenticationSystemCancel) withObject:nil waitUntilDone:YES];
                    }
                }else if (error.code == LAErrorUserCancel) {
                    [self performSelectorOnMainThread:@selector(localAuthenticationCancel) withObject:nil waitUntilDone:YES];
                }else{
                        [self performSelectorOnMainThread:@selector(localAuthenticationFailed) withObject:nil waitUntilDone:YES];
                }
                
            }
        }];
        return YES;
    }
    return NO;
}

- (void)localAuthenticationSystemCancel{
    
}

- (void)localAuthenticationCancel{
    
}

- (void)localAuthenticationFailed{
    
}

- (void)localAuthenticationSuccess{
    
}

@end
